package com.rrd.mex.dw.mr.babel.parser;

import com.alibaba.fastjson.JSONObject;
import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.mexico.MultiPlatformOverdueRecord;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;

/**
 * @Classname MultiPlatformOverdueRecordParse
 * @Description 多平台逾期检测
 * @Date 2023/8/8 16:08
 * @Created by zxb
 */

public class MultiPlatformOverdueRecordParse extends ParseBase {
    Text key = new Text();
    Text value = new Text();

    @Override
    public boolean parseJob(Mapper.Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        MultiPlatformOverdueRecord obj = (MultiPlatformOverdueRecord) model;
        StringBuilder sb = getBaseInfo(obj, successful,1);

        sb.append(obj.getCode() == null ? "" : obj.getCode()).append(CRT001);
        sb.append(obj.getMessage() == null ? "" : obj.getMessage()).append(CRT001);
        sb.append(obj.getTransactionId() == null ? "" : obj.getTransactionId()).append(CRT001);
        sb.append(obj.getPricingStrategy() == null ? "" : obj.getPricingStrategy()).append(CRT001);
        sb.append(obj.getExtra() == null ? "" : obj.getExtra()).append(CRT001);

        JSONObject jobj = obj.getData();
        if (jobj==null){
            for (int i=1;i<=59;i++){
                sb.append("").append(CRT001);
            }
        }else {
            for (int i=1;i<=59;i++){
                sb.append(jobj.getString("MPOD_"+i) == null ? "" : jobj.getString("MPOD_"+i)).append(CRT001);
            }

        }

        key.set(getKey());
        value.set(sb.toString().replace("\n", " ").replace("\r", " "));
        context.write(key, value);
        return true;
    }

    @Override
    public String getTableName() {
        return "kafka_babel_multi_platform_overdue_inc";
    }

    @Override
    public String getType() {
        return RecordType.MULTI_PLATFORM_OVERDUE_RECORD.toString();
    }

    @Override
    public String getComment() {
        return "多平台逾期检测";
    }

    @Override
    public BaseModel getModel() {
        return new MultiPlatformOverdueRecord();
    }
}
